/*
 * Copyright © YOLANDA. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nohttp;

import android.content.Context;

import com.bigkoo.svprogresshud.SVProgressHUD;
import com.yolanda.nohttp.rest.OnResponseListener;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;



/**
 * Created in Nov 4, 2015 12:02:55 PM
 *
 * @author YOLANDA
 */
public class HttpResponseListener<T> implements OnResponseListener<T> {
    private SVProgressHUD progress;
    /**
     * Dialog
     */
    private WaitDialog mWaitDialog;

    /**
     * 当前请求
     */
    private Request<?> mRequest;

    /**
     * 结果回调
     */
    private HttpListener<T> callback;

    /**
     * 是否显示dialog
     */
    private boolean isLoading;

    /**
     * @param context      context用来实例化dialog
     * @param request      请求对象
     * @param httpCallback 回调对象
     * @param canCancel    是否允许用户取消请求
     * @param isLoading    是否显示dialog
     */
    public HttpResponseListener(Context context, Request<?> request, HttpListener<T> httpCallback, boolean canCancel, boolean isLoading) {
        this.mRequest = request;
        if (context != null && isLoading) {
            if (progress == null) {
                progress = new SVProgressHUD(context);
            }
            progress.showWithStatus("加载中...");
        }
        this.callback = httpCallback;
        this.isLoading = isLoading;
    }

    /**
     * 开始请求, 这里z'z显示一个dialog
     */
    @Override
    public void onStart(int what) {
        if (progress != null) {
            progress.dismiss();
        }
    }

    /**
     * 结束请求, 这里关闭dialog
     */
    @Override
    public void onFinish(int what) {
//        if (isLoading && mWaitDialog != null && mWaitDialog.isShowing())
//            mWaitDialog.dismiss();
    }

    /**
     * 成功回调
     */
    @Override
    public void onSucceed(int what, Response<T> response) {
        if (callback != null)
            callback.onSucceed(what, response);
    }

   /* @Override
    public void onFailed(int what, Response<T> response) {

    }*/

    /**
     * 失败回调
     */
    public void onFailed(int what, String url, Object tag, Exception e, int responseCode, long networkMillis) {
        if (callback != null)
            callback.onFailed(what, url, tag, e, responseCode, networkMillis);
    }

}
